<?php
/**
 * Widget管理
 *
 * @package  Apps
 * @author   Wing <wxywing@gmail.com>
 * @version  $Id: WidgetAction.class.php Thu Apr 19 06:37:57 GMT 2012 wing $ 
 */
class WidgetAction extends AdministratorAction
{
	public function index()
	{
	    $model = model('Widget');
        $result = $model->getWidgetList();
		$this->assign('data',$result);
		$this->assign('widget_types', C('widget_types'));
		$this->display();
	}
	
    public function addWidget() 
    {
    	$this->assign('widget_types', C('widget_types'));
    	$this->assign('type', 'add');
        $this->display('editWidget');
    }
    
    public function doAddWidget() 
    {
    	//参数合法性检查
		$required_field = array(
			'widget_name'	=> '组件名称',
			'widget_alias'	=> '组件别名',
		);
		foreach ($required_field as $k => $v) {
			if ( empty($_POST[$k]) ) $this->error($v . '不可为空');
		}
		$haveName = Model('Widget')->where( "`widget_name`='".t($_POST['widget_name'])."'")->find();
		if( is_array( $haveName ) && sizeof($haveName)>0 ){
			$this->error('组件名称已经存在');
		}
		
		$_widget['widget_name'] = $_POST['widget_name'];
		$_widget['widget_alias']= $_POST['widget_alias'];
		$_widget['status'] = intval($_POST['status']);
		$_widget['description'] = $_POST['description'];
		$_widget['widget_type'] = $_POST['widget_type'];
		//预留
		$_widget['params'] = isset($_POST['params']) ? $_POST['params'] : '';
		$_widget['role_id'] = 0;
		$id = Model('Widget')->add($_widget);
		if (!$id) {
			$this->error('抱歉：添加失败，请稍后重试');
			exit;
		}

		$this->assign('jumpUrl', U('admin/Widget/index')); 
		$this->success('添加成功');
    }
    
    public function editWidget() 
    {
    	$id  = intval($_GET['id']);
    	if (empty($id)) $this->error('参数错误');
    	
    	$widget = Model('widget')->getWidgetInfo($id);
    	if(!$widget) $this->error('无此组件');

    	$this->assign($widget);
    	$this->assign('widget_types', C('widget_types'));
    	$this->assign('type', 'edit');
    	$this->display();
    }
    
    public function doEditWidget() 
    {
    	$_POST['id']	= intval($_POST['id']);

		//参数合法性检查
		$required_field = array(
			'widget_name'	=> '组件名称',
			'widget_alias'	=> '组件别名',
		);
		foreach ($required_field as $k => $v) {
			if ( empty($_POST[$k]) ) $this->error($v . '不可为空');
		}
		//保存修改
		$key   		= array('widget_name', 'widget_alias', 'status', 'description', 'widget_type', 'params', 'role_id');
		$value 		= array($_POST['widget_name'], $_POST['widget_alias'], intval($_POST['status']), $_POST['description'], $_POST['widget_type'], $_POST['params'], 0);
		$map['id']	= $_POST['id'];

		$res = Model('widget')->where($map)->setField($key, $value);
		$this->assign('jumpUrl', U('admin/Widget/index'));
		$this->success('保存成功');
    }
    
    public function doDeleteWidget() 
    {
    	$ids = t($_POST['id']);
    	$ids = explode(',', $ids);
    
    	$map['id'] = array('in', $ids);
    	$res = model('widget')->where($map)->delete();
    	if ($res){
    		echo 1;		  
    	} else{
    	 	echo 0;
    	}
    	exit();
    }
}